package Work4.Slay_the_Maze.Events;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import Work4.Slay_the_Maze.Cards.*;
import Work4.Slay_the_Maze.Player.*;
import Work4.Slay_the_Maze.Potions.*;
import Work4.Slay_the_Maze.Relics.*;

public class Shop extends Events {
	public Shop() {
		super("商店","你遇到了一个商店");
	}
	private ArrayList<Cards> cards =new ArrayList<Cards>();
	private ArrayList<Potions> potions =new ArrayList<Potions>();
	private ArrayList<Relics> relics =new ArrayList<Relics>();
	public void CreatShop() {
		CardsPool cardspool=new CardsPool();
		PotionsPool potionspool=new PotionsPool();
		Relicspool relicspool=new Relicspool();
		for(int i=0;i<4;i++) {
			cards.add(cardspool.RandomgetCard());
		}
		for(int i=0;i<3;i++) {
			potions.add(potionspool.RandomgetPotion());
		}
		for(int i=0;i<2;i++) {
			relics.add(relicspool.RandomgetRelic());
		}
		
	}
	public void Buyit(Player player) {
		CreatShop();
		Printinfo();
		boolean temp=false;
		Scanner sc=new Scanner(System.in);
		do {
			System.out.println("请做出选择:");
			System.out.println("0.离开");
			System.out.println("1.购买卡牌");
			System.out.println("2.购买药水");
			System.out.println("3.购买遗物");
			while (!sc.hasNextInt()) {
			    System.out.println("输入无效，请输入一个整数:");
			    sc.next(); // 清除错误输入，避免死循环
			}
			int choice1=sc.nextInt();
			int choice2=0;
			switch(choice1) {
				case 0:
					temp=true;
					break;
				case 1:
					choice2=1;
					break;
				case 2:
					choice2=2;
					break;
				case 3:
					choice2=3;
					break;
				default:
					System.out.println("输入错误");
			}
			boolean temp1=false;
			if(choice2==1) {
				do {
					System.out.println("请做出选择(输入0返回):");
					System.out.println("你的金币:"+player.getMoney()+" ");
					for(int i=0;i<cards.size();i++) {
						System.out.printf("["+(i+1)+"]");
						System.out.printf(" 价格:"+cards.get(i).getValue()+" ");
						cards.get(i).PrintInfo();
					}
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请输入一个整数:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					int choice3=sc.nextInt();
					if(choice3==0) {
						temp1=true;
					}else if(choice3>=1&&choice3<=4) {
						if(cards.size()>=choice3)
						{
							if(player.getMoney()>=cards.get(choice3-1).getValue()) {
								player.setMoney(player.getMoney()-cards.get(choice3-1).getValue());
								player.addhandcard(cards.get(choice3-1));
								cards.remove(choice3-1);
							}else {
								System.out.println("金币不足");
							}
						}else {
							System.out.println("无效输入");
						}
					}else {
						System.out.println("无效输入");
					}
					
				}while(!temp1);
			}else if(choice2==2) {
				do {
					System.out.println("请做出选择(输入0返回):");
					System.out.println("你的金币:"+player.getMoney()+" ");
					for(int i=0;i<potions.size();i++) {
						System.out.printf("["+(i+1)+"]");
						System.out.printf(" 价格:"+potions.get(i).getValue()+" ");
						potions.get(i).PrintInfo();
					}
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请输入一个整数:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					int choice3=sc.nextInt();
					if(choice3==0) {
						temp1=true;
					}else if(choice3>=1&&choice3<4) {
						if(potions.size()>=choice3)
						{
							if(player.getMoney()>=potions.get(choice3-1).getValue()) {
								player.setMoney(player.getMoney()-potions.get(choice3-1).getValue());
								player.addpotions(potions.get(choice3-1));
								potions.remove(choice3-1);
							}else {
								System.out.println("金币不足");
							}
						}else {
							System.out.println("无效输入");
						}
					}else {
						System.out.println("无效输入");
					}
					
				}while(!temp1);
			}else if(choice2==3) {
				do {
					System.out.println("请做出选择(输入0返回):");
					System.out.println("你的金币:"+player.getMoney()+" ");
					for(int i=0;i<relics.size();i++) {
						System.out.printf("["+(i+1)+"]");
						System.out.printf(" 价格:"+relics.get(i).getValue()+" ");
						relics.get(i).PrintInfo();
					}
					while (!sc.hasNextInt()) {
					    System.out.println("输入无效，请输入一个整数:");
					    sc.next(); // 清除错误输入，避免死循环
					}
					int choice3=sc.nextInt();
					if(choice3==0) {
						temp1=true;
					}else if(choice3>=1&&choice3<3) {
						if(relics.size()>=choice3)
						{
							if(player.getMoney()>=relics.get(choice3-1).getValue()) {
								player.setMoney(player.getMoney()-relics.get(choice3-1).getValue());
								player.addrelics(player,relics.get(choice3-1));
								relics.remove(choice3-1);
							}else {
								System.out.println("金币不足");
							}
						}else {
							System.out.println("无效输入");
						}
					}else {
						System.out.println("无效输入");
					}
					
				}while(!temp1);
			}
		}while(!temp);
	}
	public void doit(Player player) {
		Buyit(player);
	}
}
